﻿#pragma warning(disable:4996)
#include <graphics.h>              // 引用图形库头文件
#include <conio.h>
#include <stdio.h>
#include <windows.h>               // 用到了定时函数Sleep()
#include <math.h>
#include <string.h>
#include <limits.h>                // 用于INT_MIN和INT_MAX

#define PI 3.1416

typedef struct node {
    struct node* next;
    int a;
} Node;

typedef struct list {
    Node* head;
} List;

// 按钮坐标
int r[3][4] = { {30,20,130,60},{170,20,220,60},{260,20,310,60} };

float x, y; // x和y

// 按钮判断函数
int button_judge(int x, int y) {
    if (x > r[0][0] && x < r[0][2] && y > r[0][1] && y < r[0][3]) return 1;
    if (x > r[1][0] && x < r[1][2] && y > r[1][1] && y < r[1][3]) return 2;
    if (x > r[2][0] && x < r[2][2] && y > r[2][1] && y < r[2][3]) return 3;
    return 0;
}

// 初始化图像
void init_figure() {
    setrop2(R2_COPYPEN); // 当前颜色
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID); // 实线
    rectangle(30, 100, 420, 330); // 外框线
    setlinestyle(PS_DOT); // 点线
    for (int i = 30 + 39; i < 420; i += 39) {
        line(i, 100, i, 330); // 竖直辅助线
    }
    for (int i = 100 + 23; i < 330; i += 23) {
        line(30, i, 420, i); // 水平辅助线
    }
}

// 全局链表变量
List list = { NULL };

// 读取用户输入并插入到链表中
void read_numbers() {
    char s[256]; // 输入字符串变量
    InputBox(s, sizeof(s), "请输入数字“n”并输入n个数字");

    // 解析输入的字符串并将其添加到链表中
    char* token = strtok(s, " ");
    while (token != NULL) {
        int num;
        if (sscanf(token, "%d", &num) == 1) {
            Node* p = (Node*)malloc(sizeof(Node));
            p->a = num;
            p->next = NULL;

            if (list.head == NULL) {
                list.head = p;
            }
            else {
                Node* last = list.head;
                while (last->next != NULL) {
                    last = last->next;
                }
                last->next = p;
            }
        }
        token = strtok(NULL, " ");
    }
}

// 仿真运行，寻找最大最小值
int calculation() {
    if (list.head == NULL) {
        outtextxy(50, 80, "没有输入数据");
        return -1;
    }

    int max = INT_MIN, min = INT_MAX, index1 = -1, index2 = -1;
    Node* u;
    int g = 1;

    for (u = list.head; u != NULL; u = u->next) {
        if (u->a > max) {
            max = u->a;
            index1 = g;
        }
        if (u->a < min) {
            min = u->a;
            index2 = g;
        }
        ++g;
    }

    char s2[100];
    sprintf(s2, "max=%d, pos=%d min=%d, pos=%d", max, index1, min, index2);
    outtextxy(50, 80, s2);

    return 0;
}

int main() {
    int event = 0;
    int win_width = 480, win_height = 360; // 定义窗口的宽度和高度

    initgraph(win_width, win_height); // 初始化窗口（黑屏）

    // 渐变背景色
    for (int i = 0; i < 256; i += 5) {
        setbkcolor(RGB(i, i, i)); // 设置背景色
        cleardevice(); // 清屏
        Sleep(30); // 延时30ms
    }

    // 画按钮
    RECT R1 = { r[0][0],r[0][1],r[0][2],r[0][3] };
    RECT R2 = { r[1][0],r[1][1],r[1][2],r[1][3] };
    RECT R3 = { r[2][0],r[2][1],r[2][2],r[2][3] };
    LOGFONT f;
    gettextstyle(&f);
    _tcscpy(f.lfFaceName, _T("宋体"));
    f.lfQuality = ANTIALIASED_QUALITY;
    settextstyle(&f);
    settextcolor(BLACK);
    drawtext("输入参数", &R1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    drawtext("计算", &R2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    drawtext("退出", &R3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    setlinecolor(BLACK);
    rectangle(r[0][0], r[0][1], r[0][2], r[0][3]);
    rectangle(r[1][0], r[1][1], r[1][2], r[1][3]);
    rectangle(r[2][0], r[2][1], r[2][2], r[2][3]);

    MOUSEMSG m;

    while (true) {
        m = GetMouseMsg();

        switch (m.uMsg) {
        case WM_MOUSEMOVE:
            setrop2(R2_XORPEN);
            setlinecolor(LIGHTCYAN);
            setlinestyle(PS_SOLID, 3);
            setfillcolor(BLUE);
            if (button_judge(m.x, m.y) != 0) {
                if (event != button_judge(m.x, m.y)) {
                    event = button_judge(m.x, m.y);
                    fillrectangle(r[event - 1][0], r[event - 1][1], r[event - 1][2], r[event - 1][3]);
                }
            }
            else {
                if (event != 0) {
                    fillrectangle(r[event - 1][0], r[event - 1][1], r[event - 1][2], r[event - 1][3]);
                    event = 0;
                }
            }
            break;
        case WM_LBUTTONDOWN:
            setrop2(R2_NOTXORPEN);
            for (int i = 0; i <= 10; i++) {
                setlinecolor(RGB(25 * i, 25 * i, 25 * i));
                circle(m.x, m.y, 2 * i);
                Sleep(20);
                circle(m.x, m.y, 2 * i);
            }
            switch (button_judge(m.x, m.y)) {
            case 1:
                read_numbers();
                FlushMouseMsgBuffer();
                break;
            case 2:
                calculation();
                FlushMouseMsgBuffer();
                break;
            case 3:
                closegraph();
                exit(0);
            default:
                FlushMouseMsgBuffer();
                break;
            }
            break;
        }
    }

    return 0;
}